package concurrencyPractice.chapter03;

import java.math.BigInteger;
import java.util.Arrays;

/**
 * @author xinya
 * @version 1.0
 * @project javacore
 * @description 程序3-12 对数值及其因数分解结果进行缓存的不可变容器类
 * @date 2023/12/27 14:51:56
 */
@Immutable
public class OneValueCache {

    private final BigInteger lastNumber;
    private final BigInteger[] lastFactors;

    public OneValueCache(BigInteger i, BigInteger [] factors) {
        lastNumber = i;

        lastFactors = Arrays.copyOf(factors, factors.length);
    }

    public BigInteger[] getFactors(BigInteger i) {
        if(lastNumber == null || !lastNumber.equals(i)) {
            return  null;
        } else {
            return Arrays.copyOf(lastFactors, lastFactors.length);
        }

    }
}
